﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Vortex.Drawing {
	/// <summary>
	/// Contains helper method to work with images and pixels
	/// </summary>
	public static class ImageHelper {

		/// <summary>
		/// Finds the matching <see cref="System.Drawing.Imaging.PixelFormat"/> for <see cref="Vortex.Drawing.Format"/>.
		/// </summary>
		/// <param name="format">The format to convert.</param>
		/// <returns>A matching <see cref="System.Drawing.Imaging.PixelFormat"/> value</returns>
		public static System.Drawing.Imaging.PixelFormat FindMatchingFormat(PixelFormat format) {
			switch (format) {
				case PixelFormat.A8R8G8B8: return System.Drawing.Imaging.PixelFormat.Format32bppArgb;
				case PixelFormat.X8R8G8B8: return System.Drawing.Imaging.PixelFormat.Format32bppRgb;
				case PixelFormat.R5G6B5: return System.Drawing.Imaging.PixelFormat.Format16bppRgb565;
				default: return System.Drawing.Imaging.PixelFormat.Undefined;
			}
		}

		/// <summary>
		/// Finds the matching <see cref="Vortex.Drawing.Format"/> for <see cref="System.Drawing.Imaging.PixelFormat"/> suitable for texture creation.
		/// </summary>
		/// <param name="format">The format to convert.</param>
		/// <returns>A matching <see cref="Vortex.Drawing.Format"/> value</returns>
		public static PixelFormat FindMatchingTextureFormat(System.Drawing.Imaging.PixelFormat format) {
			switch (format) {
				case System.Drawing.Imaging.PixelFormat.Format32bppArgb: return PixelFormat.A8R8G8B8;
				case System.Drawing.Imaging.PixelFormat.Format24bppRgb: return PixelFormat.X8R8G8B8;
				case System.Drawing.Imaging.PixelFormat.Format32bppRgb: return PixelFormat.X8R8G8B8;
				case System.Drawing.Imaging.PixelFormat.Format16bppRgb565: return PixelFormat.R5G6B5;
				default: return PixelFormat.Unknown;
			}
		}

		/// <summary>
		/// Finds the matching <see cref="Vortex.Drawing.Format"/> for <see cref="System.Drawing.Imaging.PixelFormat"/>.
		/// </summary>
		/// <param name="format">The format to convert.</param>
		/// <returns>A matching <see cref="Vortex.Drawing.Format"/> value</returns>
		public static PixelFormat FindMatchingFormat(System.Drawing.Imaging.PixelFormat format) {
			switch (format) {
				case System.Drawing.Imaging.PixelFormat.Format32bppArgb: return PixelFormat.A8R8G8B8;
				case System.Drawing.Imaging.PixelFormat.Format24bppRgb: return PixelFormat.R8G8B8;
				case System.Drawing.Imaging.PixelFormat.Format32bppRgb: return PixelFormat.X8R8G8B8;
				case System.Drawing.Imaging.PixelFormat.Format16bppRgb565: return PixelFormat.R5G6B5;
				default: return PixelFormat.Unknown;
			}
		}



		/// <summary>
		/// Gets the size of the pixel for specified format.
		/// </summary>
		/// <param name="format">The format.</param>
		/// <returns>Size of pixel for specified format or 0 if format is unknown</returns>
		public static int GetPixelSize(PixelFormat format) {
			switch (format) {
				case PixelFormat.A32B32G32R32F: return 128;
				case PixelFormat.G32R32F: return 64;
				case PixelFormat.X8R8G8B8:
				case PixelFormat.A8R8G8B8: 
				case PixelFormat.R32F: return 4;
				case PixelFormat.R8G8B8: return 3;
				case PixelFormat.A1R5G5B5:
				case PixelFormat.A4R4G4B4:
				case PixelFormat.R5G6B5: return 2;
				default: return 0;
			}
		}
	}
}
